package com.example.mlm;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.example.pojos.Product;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Detail_musique extends Activity implements OnClickListener{
	private Product product;
	private Button startButton;
	private Button stopButton;
	private TextView titre;
	private TextView description;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_musique2);
		
		if(product==null)
			product = (Product) getIntent().getExtras().get("product");

		startButton = (Button) findViewById(R.id.start);
		stopButton = (Button) findViewById(R.id.stop);
		titre = (TextView) findViewById(R.id.titre);
		description = (TextView) findViewById(R.id.description);
		
		startButton.setOnClickListener(this);
		stopButton.setOnClickListener(this);
		titre.setText(product.getName());
		description.setText(product.getLongDesc());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_detail_musique, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.menu_settings:
	        	modify_pan(false);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@SuppressWarnings("unchecked")
	public void modify_pan(boolean remove){
		List<Product> panier;
		try {
            FileInputStream fStream = openFileInput("panier.bin") ;
            ObjectInputStream oStream = new ObjectInputStream(fStream);

            panier = (List<Product>) oStream.readObject();        
            oStream.close();
        } catch (Exception e) {
        	panier = new ArrayList<Product>();
            Log.e("IO Exception", e.getMessage());
        }
		if(!remove){
			if(!panier.contains(product))
				panier.add(product);
		}
		else{
			panier.remove(product);
		}
        try {
            FileOutputStream fStream = openFileOutput("panier.bin", Context.MODE_PRIVATE) ;
            ObjectOutputStream oStream = new ObjectOutputStream(fStream);

            oStream.writeObject(panier);        
            oStream.flush();
            oStream.close();

            Log.e("Serialization success", "Success panier");
        } catch (Exception e) {
            Log.e("IO Exception", e.getMessage());
        }
	}

	@Override
	public void onClick(View v) {
		if(v==startButton){
			Intent intent = new Intent(this, MyPlayer.class);
			intent.putExtra("musique", product.getUrl());
			startService(intent);
		}
		else if(v==stopButton){
			stopService(new Intent(this, MyPlayer.class));
		}
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putParcelable("product", product);
	}
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		product = savedInstanceState.getParcelable("product");
	}

}
